﻿using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using System.Security.Claims;
using YunZiChanApi.Yunbo;

namespace YunZiChanApi.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class AdminController : BaseController
    {
        private readonly IConfiguration _configuration;

        public AdminController(IConfiguration configuration)
        {
            _configuration = configuration;
        }

        /// <summary>
        /// 接口测试首页，显示版本号
        /// </summary>
        /// <returns></returns>
        [Route("Index"), HttpGet]
        public IActionResult Index()
        {
            return Success("Version：1.0.0 230703");
        }

        /// <summary>
        /// 用户登入获取Token
        /// </summary>
        /// <returns></returns>
        [Route("Login"), HttpGet]
        public IActionResult Login()
        {
            //验证用户账号密码
            //.......


            // 1. 定义需要使用到的Claims
            var claims = new[]
            {
                //把用户的id加密进去，使用的时候可以从token里解密拿出来
                new Claim("Id", "1"),
                //也可以多传几个参数
                new Claim("UserName", "tiaoma888")
            };
            // 实例化TokenHelper对象
            TokenHelper tokenHelper = new(_configuration);
            // 生成Token
            string jwtToken = tokenHelper.GenerateToken(claims);


            return Success(jwtToken);
        }
    }
}
